<?php
/**
 * Created by PhpStorm.
 * User: 朱杰
 * Date: 2017/2/25
 * Time: 14:40
 */

namespace app\controllers;

use app\models\Find;
use app\models\Find_hint;
use app\models\Get;
use app\models\Get_hint;
use app\models\Users;
use Yii;
use yii\filters\AccessControl;
use yii\web\Controller;
use yii\filters\VerbFilter;
use app\models\LoginForm;
use app\models\ContactForm;
use yii\db\Query;

class DetailsController extends Controller
{
    /**
     * @inheritdoc
     */

    public function behaviors()
    {
        return [
            'access' => [
                'class' => AccessControl::className(),
                'only' => ['logout'],
                'rules' => [
                    [
                        'actions' => ['logout'],
                        'allow' => true,
                        'roles' => ['@'],
                    ],
                ],
            ],
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'logout' => ['post'],
                ],
            ],
        ];
    }

    /**
     * @inheritdoc
     */
    public function actions()
    {
        return [
            'error' => [
                'class' => 'yii\web\ErrorAction',
            ],
            'captcha' => [
                'class' => 'yii\captcha\CaptchaAction',
                'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
            ],
        ];
    }

    /**
     * Displays homepage.
     *
     * @return string
     */
    public function actionFind_details()
    {
        $request=Yii::$app->request;
        $session=Yii::$app->session;
        $id=$request->get("id");
        $row=Get::findOne($id);
        $hints=null;
        //post上来用户发布的信息,保存成功返回1，失败返回2
        if ($request->isPost){
            if (!isset($session["username"])){
                return $this->redirect("?r=site/signin");
            }

            $content=$request->post("message");

            $time=time();
            $newMes=new Get_hint();
            $newMes->content=$content;
            $newMes->time=$time;
            $newMes->get_id1=intval($id);
            if ($newMes->save()){
                //该物品的线索
                $hints=(new Query())
                    ->select(["content","time"])
                    ->from("get_hint")
                    ->where(["get_id1"=>$id])
                    ->orderBy("time desc")
                    ->all();
                return $this->render('find_details',["find"=>$row,"mes"=>1,"hints"=>$hints]);
            }else{
                //该物品的线索
                $hints=(new Query())
                    ->select(["content","time"])
                    ->from("get_hint")
                    ->where(["get_id1"=>$id])
                    ->orderBy("time desc")
                    ->all();
                return $this->render('find_details',["find"=>$row,"mes"=>2,"hints"=>$hints]);
            }
        }
        //该物品的线索
        $hints=(new Query())
            ->select(["content","time"])
            ->from("get_hint")
            ->where(["get_id1"=>$id])
            ->orderBy("time desc")
            ->all();
        return $this->render('find_details',["find"=>$row,"mes"=>0,"hints"=>$hints]);
    }

    public function actionLost_details()
    {
        $request=Yii::$app->request;
        $session=Yii::$app->session;
        $id=$request->get("id");
        $row=Find::findOne($id);
        $hints=null;

        //post上来用户发布的信息,保存成功返回1，失败返回2
        if ($request->isPost){
            if (!isset($session["username"])){
                return $this->redirect("?r=site/signin");
            }
            $content=$request->post("message");
            $time=time();
            $newMes=new Find_hint();
            $newMes->content=$content;
            $newMes->time=$time;
            $newMes->find_id1=intval($id);
            if ($newMes->save()){

                //该物品的线索
                $hints=(new Query())
                    ->select(["content","time"])
                    ->from("find_hint")
                    ->where(["find_id1"=>$id])
                    ->orderBy("time desc")
                    ->all();
                return $this->render('lost_details',["lost"=>$row,"mes"=>1,"hints"=>$hints]);
            }else{

                //该物品的线索
                $hints=(new Query())
                    ->select(["content","time"])
                    ->from("find_hint")
                    ->where(["find_id1"=>$id])
                    ->orderBy("time desc")
                    ->all();
                return $this->render('lost_details',["lost"=>$row,"mes"=>2,"hints"=>$hints]);
            }
        }

        //该物品的线索
        $hints=(new Query())
            ->select(["content","time"])
            ->from("find_hint")
            ->where(["find_id1"=>$id])
            ->orderBy("time desc")
            ->all();

        return $this->render('lost_details',["lost"=>$row,"mes"=>0,"hints"=>$hints]);
    }

    /**
     * Login action.
     *
     * @return string
     */

    public function actionLogin()
    {
        if (!Yii::$app->user->isGuest) {
            return $this->goHome();
        }

        $model = new LoginForm();
        if ($model->load(Yii::$app->request->post()) && $model->login()) {
            return $this->goBack();
        }
        return $this->render('login', [
            'model' => $model,
        ]);
    }

    /**
     * Logout action.
     *
     * @return string
     */
    public function actionLogout()
    {
        Yii::$app->user->logout();

        return $this->goHome();
    }

    /**
     * Displays contact page.
     *
     * @return string
     */
    public function actionContact()
    {
        $model = new ContactForm();
        if ($model->load(Yii::$app->request->post()) && $model->contact(Yii::$app->params['adminEmail'])) {
            Yii::$app->session->setFlash('contactFormSubmitted');

            return $this->refresh();
        }
        return $this->render('contact', [
            'model' => $model,
        ]);
    }

    /**
     * Displays about page.
     *
     * @return string
     */
    public function actionAbout()
    {
        return $this->render('about');
    }
}
